CentOS 6
Sponsored Link

LVS + Keepalived #1
2013/08/20
 
Maybe packets are forwarded to a server even if it is down for only the configuration of LVS, so it's better to use Keepalived.
This example shows to configure it on the environment below.
                          |
                       VIP|192.168.0.99
                      eth0|192.168.0.100
                    +----------------+
--------------------| LVS+Keepalived |----------------------
                    +----------------+
                      eth1|10.0.0.100
                          |
+----------+              |               +----------+
|          |10.0.0.30     |      10.0.0.31|          |
| Backend1 |------------------------------| Backend2 |
|   Web    | eth0                     eth0|    Web   |
+----------+                              +----------+

 
Set VIP on Keepalived Server and Packets to the VIP are forwarded to Backend1 and Backend2 Servers with NAT.
Please change the default gateway to internal IP address of Keepalived Server on both Backend Web Servers first. (it's 10.0.0.100 on this example)
[1] Clear the LVS settings first because it is controled by Keepalived.
[root@gw ~]#
ipvsadm -C

[root@gw ~]#
/etc/rc.d/init.d/ipvsadm save

ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [ OK ]
[2] Install Keepalived
[root@gw ~]#
yum -y install keepalived
[3] Configure Keepalived
[root@gw ~]#
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bk

[root@gw ~]#
vi /etc/keepalived/keepalived.conf
# create new

global_defs {
    notification_email {
# notification destination

        cent@srv.world
    }
# notification source

    notification_email_from root@srv.world
# mail server to use

    smtp_server 127.0.0.1
    smtp_connect_timeout 30
# any name you like

    router_id LVS_Server
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
# virtual router's ID

    virtual_router_id 51

    virtual_ipaddress {
# virtual IP

        192.168.0.99
    }
}
virtual_server 192.168.0.99 80 {
    delay_loop 3
# distribution method

    lvs_sched rr
# use NAT

    lvs_method NAT
    protocol TCP
# backend #1

    real_server 10.0.0.30 80 {
        weight 1
        HTTP_GET {
            url {
                path /
                status_code 200
            }
            connect_timeout 3
        }
    }
# backend #2

    real_server 10.0.0.31 80 {
        weight 1
        HTTP_GET {
            url {
                path /
                status_code 200
            }
            connect_timeout 3
        }
    }
}

[root@gw ~]#
/etc/rc.d/init.d/keepalived start

Starting keepalived: [ OK ]
[root@gw ~]#
chkconfig keepalived on
# make sure VIP with the command below (it's not shown by ifconfig

[root@gw ~]#
ip addr show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:16:36:92:b5:dd brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.100/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.99/32 scope global eth0
    inet6 fe80::216:36ff:fe92:b5dd/64 scope link
       valid_lft forever preferred_lft forever
[4]
It's OK all. Access to the Service IP address and make sure it works normally.
 
Tweet